//入口函数
$(function () {
    getuserinfo()
    //退出登录
    $('#logout').on('click', function () {
        //eg1
        layer.confirm('确定退出登录吗？', { icon: 3, title: '提示' }, function (index) {
            //do something
            location.href = '/login.html'
            localStorage.removeItem('token')

        });

        layer.close(index);


    })
});

function getuserinfo() {
    axios({
        //默认为get,可以省略
        method: 'GET',
        url: '/my/userinfo',
        //以 /my 开头的请求路径，需要在请求头中携带 
        //Authorization 身份认证字段，才能正常访问成功
        // headers: {
        //     Authorization: localStorage.getItem('token')
        // }
    }).then(res => {
        //console.log(this);//this指向window
        // console.log(res.data);
        if (res.data.status !== 0) {
            return layui.layer.msg(res.data.message)
        }
        //console.log(res.data)
        renderavatar(res.data.data)
    })



}
function renderavatar(user) {
    let name = user.nickname || user.username
    $('.welcome').html('欢迎 ' + name)
    if (user.user_pic != null) {
        $('.avater-text').hide()
        $('.layui-nav-img').show().attr('src', user.user_pic)
    }
    else {
        $('.layui-nav-img').hide()
        //第一个字母大写
        let first = name[0].toUpperCase()
        $('.avater-text').show().html(first)
    }
}